package cn.com.huzo.webapp.controller.oa;

import cn.com.huzo.model.entities.oa.BulletinEntity;
import cn.com.huzo.service.oa.BulletinManager;
import huzofw.common.web.controller.BaseFormController;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 公告板
 *
 * @author 吴旭
 */
@Controller
@RequestMapping(value = "/oa/bulletin")
public class BulletinController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">
    @Resource(name = "BulletinManager")
    private BulletinManager _manager;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 跳转到公告板一览画面
     * <p>
     * @param request
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("/list.html")
    public ModelAndView redirectListWnd(HttpServletRequest request) throws Exception {
        ModelAndView view = new ModelAndView();
        view.setViewName("oa.bulletin.list.def");
        return view;
    }

    /**
     * 跳转到公告板编辑画面
     *
     * @param action 操作方法
     * @param bulletinId 公告板ID
     * @return
     */
    @RequestMapping("/{action}/{bulletinId}/edit.html")
    public ModelAndView redirectEditPopupWnd(@PathVariable(value = "action") int action,
            @PathVariable(value = "bulletinId") String bulletinId) {
        ModelAndView view = new ModelAndView();
        // 为画面设置变量        
        view.addObject("action", action);
        if (!"0".equals(bulletinId)) {
            view.addObject("bulletinId", bulletinId);
        }
        view.setViewName("oa.bulletin.edit.def");
        return view;
    }

    /**
     * 弹出公告板 更多详细画面
     *
     * @param bulletinId 要显示的公告板ID
     * @return
     */
    @RequestMapping("/{bulletinId}/show.html")
    public ModelAndView redirectPopupWnd(@PathVariable(value = "bulletinId") String bulletinId) {
        ModelAndView view = new ModelAndView();
        // 为画面设置变量        
        view.addObject("bulletinId", bulletinId);
        view.setViewName("popup.oa.bulletin.show.def");
        return view;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="数据处理部分">
    /**
     * 取得该公告板详细。
     *
     * @param criterion 用户标识、起止时间
     * @return 公告板数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/fetchByBulletinId.html",
            method = RequestMethod.POST)
    @ResponseBody
    public BulletinEntity fetchByBulletinId(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager.fetchByKey(MapUtils.getString(criterion, "HeaderId"));
    }

    /**
     * 根据条件取得公告板信息分页数据。
     * <p>
     * @param criterion 检索条件
     * @return 分页数据
     * @throws Exception
     */
    @RequestMapping(value = "/fetchForPage.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchForPage(@RequestBody Map<String, Object> criterion) throws Exception {
        return _manager.fetchForPage(criterion);
    }

    /**
     * 保存公告板数据。
     * <p>
     * @param instance 要保存的信息对象。
     * @return 新增记录主键.
     * @throws Exception
     */
    @RequestMapping(value = "/save.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> save(@RequestBody BulletinEntity instance) throws Exception {
        Map<String, Object> rtnVal = new HashMap();
        if (!"".equals(instance.getBulletinId())) {
            if (_manager.update(instance) > 0) {
                rtnVal.put("Success", true);
            } else {
                rtnVal.put("Success", false);
            }
        } else {
            rtnVal.put("HeaderKey", _manager.insert(instance));
        }

        return rtnVal;
    }

    /**
     * 删除公告板信息数据。
     *
     * @param instance 要删除的数据
     * @return 返回删除后的数据
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer delete(@RequestBody BulletinEntity instance) throws Exception {
        return _manager.delete(instance);
    }
    // </editor-fold>
}
